package com.example.test.utils;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.test.R;

import java.util.LinkedHashMap;
import java.util.Map;

public class Sign extends AppCompatActivity {
    private EditText name;
    private EditText password;
    private EditText passwordAgain;
    private EditText classHome;
    private EditText phone;
    private EditText sex;
    private EditText username;
    private boolean status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign);
        this.init();
    }

    public void init() {
        name = findViewById(R.id.sign_user_name);
        password = findViewById(R.id.sign_user_password);
        passwordAgain = findViewById(R.id.sign_user_passwordAgin);
        classHome = findViewById(R.id.sign_user_class);
        phone = findViewById(R.id.sign_user_phone);
        sex = findViewById(R.id.sign_user_sex);
        username = findViewById(R.id.sign_userNmae);
        status = true;
    }

    public void signStart(View view) {
        if (status) {
            if (!password.getText().toString().equals(passwordAgain.getText().toString())) {
                Toast.makeText(Sign.this, "两次输入的密码不一致，请检查输入", Toast.LENGTH_LONG).show();
                return;
            } else {
                String Url = this.dataUpdate();
                if (Url.equals("false")) {
                    Toast.makeText(Sign.this, "不能为空", Toast.LENGTH_LONG).show();
                } else {
                    status = false;
                    createUser(Url);
                }
            }
        }
    }

    public String dataUpdate() {
        String[] data = { name.getText().toString(),username.getText().toString(), password.getText().toString(), classHome.getText().toString(), phone.getText().toString(), sex.getText().toString()};
        String[] dataName = {"username", "name", "password", "class", "phone", "sex"};
        StringBuilder url;
        if (MyApplication.getGlobalvariable().equals("student")) {
            url = new StringBuilder("http://36428pn632.qicp.vip/student/index.php/user/user/create");
        } else {
            url = new StringBuilder("http://36428pn632.qicp.vip/student/index.php/teacher/teacher/create");
        }
        for (int i = 0; i < data.length; i++) {
            if (data[i].equals("")) {
                return "false";
            }
            if (i == 0) {
                url.append("?").append(dataName[i]).append("=").append(data[i]);
            } else {
                url.append("&").append(dataName[i]).append("=").append(data[i]);
            }
        }
        return url.toString();
    }

    public void createUser(final String url) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String res = Http.sendGet(url);
                final LinkedHashMap<String, Object> hashMaps = Http.stringToJsonObject(res);
//                Log.d("HttpGET", (String) hashMaps.get("msg"));
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        assert hashMaps != null;
                        Toast.makeText(Sign.this, (String) hashMaps.get("msg"), Toast.LENGTH_LONG).show();
                        if ((int) hashMaps.get("code") == 1) {
                            status = true;
                            canBack();
                        } else {
                            Toast.makeText(Sign.this, "创建失败", Toast.LENGTH_LONG).show();

                        }
                    }
                });
            }
        }).start();
    }

    public void canBack() {
        Intent intent = new Intent(Sign.this, com.example.test.studentLoginActivity.class);
        //启动
        startActivity(intent);
    }
}
